<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" @submit.native.prevent>
      <el-form-item label="Code_Div：">
        <el-input v-model="listQuery.q_codeDiv" size="small" class="filter-item" maxlength="50" />
      </el-form-item>
      <el-form-item label="Code_Text：">
        <el-input v-model="listQuery.q_codeCd" size="small" class="filter-item" maxlength="50" />
      </el-form-item>
      <el-form-item label="是否使用：">
        <el-select v-model="listQuery.q_useYn" size="small" placeholder="请选择">
          <el-option v-for="item in Status" :label="item.name" :key="item.value" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="small" type="primary" @click="check">查询</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="button-table-margin">
      <el-button size="small" type="primary" @click='addCode' v-permission="['admin/system/code/create']">添加</el-button>
      <el-button size="small" type="primary" @click='editCode' v-permission="['admin/system/code/update']">修改
      </el-button>
    </div>
    <el-table ref="singleTable" :data="dataList" v-loading="listLoading" border fit highlight-current-row
      style="width: 100%" @current-change="handleCurrentChange">
      <el-table-column type="index" align="center" width="50">
      </el-table-column>
      <el-table-column property="codeDiv" width="200" label="CODE DIV" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="CODE CD" property="codeCd" show-overflow-tooltip>
      </el-table-column>
      <el-table-column property="codeText" label="CODE TEXT" show-overflow-tooltip>
      </el-table-column>
      <el-table-column property="sortOrder" label="顺序">
      </el-table-column>
      <el-table-column label="是否使用">
        <template slot-scope="scope">
          {{ codeBox.USE_YN[scope.row.useYn] }}
        </template>
      </el-table-column>
      <el-table-column property="createUser" label="创建人">
      </el-table-column>
      <el-table-column property="createTime" width="200" label="创建时间">
      </el-table-column>
      <el-table-column property="updateUser" label="修改人">
      </el-table-column>
      <el-table-column property="updateTime" width="200" label="修改时间">
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />
    <el-dialog :title="judgmentTitle" :visible.sync="dialogFormVisible" width="70%">
      <div class="app-container">
        <el-row>
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px"
            class="demo-ruleForm el-form-bottom">
            <el-col :span="24">
              <el-form-item label="CODE DIV" prop="codeDiv">
                <el-input id='codediv' v-model="ruleForm.codeDiv" :disabled="isOnly"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="CODE CD" prop="codeCd">
                <el-input-number id='codecd' v-model="ruleForm.codeCd" :disabled="isOnly" :min="1" :max="99" label="">
                </el-input-number>
                <span class="form-word-hint">最大99</span>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="CODE名称" prop="codeText">
                <el-input v-model="ruleForm.codeText"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="顺序位" prop="sortOrder">
                <el-input-number v-model="ruleForm.sortOrder" :min="0" :max="99" label=""></el-input-number>
                <span class="form-word-hint">序号越小越靠前显示 最大99</span>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="是否使用" prop="useYn">
                <el-radio-group v-model="ruleForm.useYn">
                  <el-radio v-for="item in statesList" :key="item.value" :label="item.value">{{item.name}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {
    fetchCodeList,
    delAdmin,
    freezeAdmin,
    createCode,
    toEditCode,
    updateCode
  } from '@/api/system'
  import {
    getCodeManager,
    codeToArray
  } from '@/api/all'
  import Pagination from '@/components/Pagination'
  import {
    Message
  } from 'element-ui'
  import Tinymce from '@/components/Tinymce'
  export default {
    name: 'adminuserlist',
    components: {
      Pagination
    },
    components: {
      Tinymce
    },
    data() {
      return {
        dataList: [],
        Status: [],
        total: 0,
        listLoading: false,
        isOnly: false,
        listQuery: {
          page: 1,
          limit: 20,
          q_codeDiv: '',
          q_codeCd: '',
          q_useYn: ''
        },
        currentRow: null,
        judgmentTitle: '数据字典添加',
        judgmentType: '1',
        dialogFormVisible: false,
        statesList: [],
        roles: [],
        ruleForm: {
          codeDiv: "",
          codeCd: '',
          codeText: "",
          sortOrder: '',
          useYn: ""
        },
        rules: {
          codeDiv: [{
              required: true,
              message: "请输入CodeDiv",
              trigger: "blur"
            },
            {
              max: 20,
              message: '不能超过20个字符',
              trigger: 'blur'
            }
          ],
          codeCd: [{
            required: true,
            message: "请输入CodeCd",
            trigger: "blur"
          }],
          codeText: [{
              required: true,
              message: "请输入Code名称",
              trigger: "blur"
            },
            {
              max: 20,
              message: '不能超过20个字符',
              trigger: 'blur'
            }
          ],
          sortOrder: [{
            required: true,
            message: "请输入排序号",
            trigger: "blur"
          }, ],
          useYn: [{
            required: true,
            message: "请选择是否使用",
            trigger: "change"
          }]
        },
      }
    },
    created() {
      getCodeManager('USE_YN', () => {
        this.Status = codeToArray('USE_YN');
        this.statesList = codeToArray("USE_YN");
      })
      this.getList()
    },
    methods: {
      handleCurrentChange(val) {
        // 选中当前行数据
        this.currentRow = val
      },
      // 获取列表数据
      getList() {
        this.listLoading = true
        fetchCodeList(this.listQuery).then(response => {
          this.dataList = response.data.rows
          this.total = response.data.total
          this.listLoading = false
          console.log(this.dataList)
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      check() {
        this.getList()
      },
      reset() {
        this.listQuery = {
          page: 1,
          limit: 20,
          q_codeDiv: '',
          q_codeCd: '',
          q_useYn: ''
        }
        this.getList()
      },
      addCode() {
        this.judgmentTitle = '数据字典添加'
        this.isOnly = false;
        this.judgmentType = '1'
        this.dialogFormVisible = true;
        this.resetForm('ruleForm')
      },
      editCode() {
        if (!this.currentRow) {
          Message.error('请选择操作行。')
          return;
        };
        this.isOnly = true;
        toEditCode({
          codeDiv: this.currentRow.codeDiv,
          codeCd: this.currentRow.codeCd
        }).then(res => {
          this.ruleForm = res.data.data.code
          this.ruleForm.useYn = this.ruleForm.useYn + ""
        })
        this.judgmentTitle = '数据字典编辑'
        this.judgmentType = '2'
        this.dialogFormVisible = true;
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.judgmentType == '1') {
              createCode(this.ruleForm).then(res => {
                if (!res.data.success) {
                  Message.error(res.data.message);
                  return;
                }
                Message.success('操作成功')
                this.dialogFormVisible = false;
                this.getList()
              })
            } else {
              updateCode(this.ruleForm).then(res => {
                if (!res.data.success) {
                  Message.error(res.data.message);
                  return;
                }
                Message.success('操作成功')
                this.dialogFormVisible = false;
                this.getList()
              })
            }
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
      }
    }
  }
</script>
<style scoped>
  .filter-container .el-col-6 {
    margin-bottom: 15px;
  }

  .filter-item {
    margin-bottom: 0
  }

  /deep/ .filter-container label {
    font-size: 14px;
    font-weight: 500;
    margin-right: 10px;
  }
</style>
